package com.gsafety.library.Widgets.dia;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.gsafety.library.R;


public class SweetAlertDia extends Dialog implements View.OnClickListener {
    private View mDialogView;
    private TextView mTvTitle;
    protected TextView mTvContent;
    protected TextView mBtnConfirm;
    private TextView mBtnCancel;

    private OnSweetClickListener mCancelListener;
    private OnSweetClickListener mConfirmListener;

    public SweetAlertDia(Context context) {
        this(context,true);
    }

    public SweetAlertDia(Context context, boolean cancelable) {
        super(context, R.style.alert_dialog);
        setCancelable(cancelable);
        setCanceledOnTouchOutside(false);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dia_alert);
        mDialogView = getWindow().getDecorView().findViewById(android.R.id.content);
        mTvTitle = (TextView) findViewById(R.id.dia_tv_title);
        mTvContent = (TextView) findViewById(R.id.dia_tv_content);
        mBtnConfirm = (TextView) findViewById(R.id.dia_btn_confirm);
        mBtnCancel = (TextView) findViewById(R.id.dia_btn_cancel);

        mBtnConfirm.setOnClickListener(this);
        mBtnCancel.setOnClickListener(this);
    }

    public SweetAlertDia setTitleText(String text) {
        if (mTvTitle != null) {
            mTvTitle.setText(TextUtils.isEmpty(text) ? "" : Html.fromHtml(text));
            mTvTitle.setVisibility(TextUtils.isEmpty(text) ? View.GONE : View.VISIBLE);
        }
        return this;
    }

    public SweetAlertDia setContentText(String text) {
        if (mTvContent != null && !TextUtils.isEmpty(text)) {
            mTvContent.setText(Html.fromHtml(text.replaceAll("\n", "<br/>")));
        }
        return this;
    }

    public SweetAlertDia showCancelButton(boolean isShow) {
        if (mBtnCancel != null) {
            mBtnCancel.setVisibility(isShow ? View.VISIBLE : View.GONE);
        }
        return this;
    }

    public SweetAlertDia setCancelText(String text) {
        if (mBtnCancel != null && !TextUtils.isEmpty(text)) {
            mBtnCancel.setText(Html.fromHtml(text));
        }
        return this;
    }

    public SweetAlertDia setConfirmText(String text) {
        if (mBtnConfirm != null && !TextUtils.isEmpty(text)) {
            mBtnConfirm.setText(Html.fromHtml(text));
        }
        return this;
    }

    public SweetAlertDia setCancelListener(OnSweetClickListener listener) {
        mCancelListener = listener;
        return this;
    }

    public SweetAlertDia setConfirmListener(
            OnSweetClickListener listener) {
        mConfirmListener = listener;
        return this;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.dia_btn_cancel) {
            if (mCancelListener != null) {
                mCancelListener.onClick(this);
            }
            dismiss();
        } else if (v.getId() == R.id.dia_btn_confirm) {
            if (mConfirmListener != null) {
                mConfirmListener.onClick(this);
            }
            dismiss();
        }
    }
}
